package unipi.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.User;
import unipi.helpers.HTMLParts;
import unipi.helpers.LoginCookie;
import unipi.helpers.Validator;

public class Login extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	public Login()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	
	}
	
	private String validateInputs(String username, String password)
	{
		if(!Validator.isValid(username) || !Validator.isValid(password))
			return "Οι χαρακτήρες ', \" και & δεν επιτρέπονται στα πεδία εισόδου.";
		
		return null;			
	}
	
	private String getPage(String errorMsg)
	{
		String title = "Σύνδεση";
		String css = HTMLParts.getCSSLink("css/login.css") + HTMLParts.getCSSLink("css/properties.css");
		String html = HTMLParts.DOCTYPE + "\n<html xmlns=\"http://www.w3.org/1999/xhtml\">" + HTMLParts.getHead(title, css, "");
		
		html += "<body> " +
				"<div id='loginWrap'> " +
				" <div id='loginMain'> " +
				"  <div id='loginLogin'></div> " +
				"  <div id='loginhead'></div> " +
				"  <div id='loginform'> " +
				" <form method=\"post\" action=\"Login\"> " +
				"  <input class='input' name='username' type='text' value='Όνομα Χρήστη' onfocus='if(this.value==\"Όνομα Χρήστη\")this.value=\"\"' onblur='if(this.value==\"\")this.value=\"Όνομα Χρήστη\"' />" +
				"  <input class='input' name='password' type='password' value='Κωδικός' onfocus='if(this.value==\"Κωδικός\")this.value=\"\"' onblur='if(this.value==\"\")this.value=\"Κωδικός\"' />" + 
				"   <input class='button'name=\"Submit\" type=\"submit\" value=\"Είσοδος\"/> " +
				"   <div class='list'> " +
				"	 να με θυμάσαι <input type='checkbox' name='rememberme' />" +
				"    <br/> "+
				"    <a href='#'>ξεχάσατε το κωδικό σας;</a> " +
				"    <br/> " +
				"    <a href='Registration'>δημιουργία λογαριασμού</a>" +
				"    <br/> " +
				"	 <span class='info left styleNone'>" + errorMsg + "</span>"+
				"  </div></form> " +
				" </div> " +
				"</div> " +
				"</div> " +
				"</body> " +
				"</html>";
						
		return html;
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		request.setCharacterEncoding("utf-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String remember = request.getParameter("rememberme");

		boolean rememberMe = (remember == null)?false:true;
		
		response.setContentType("text/html; charset=UTF-8");	
		PrintWriter out = response.getWriter();
		
		String validation = validateInputs(username, password);
		
		if(validation == null)
		{			
			String uid = User.authUser(username, password);
			
			if(uid == null)
			{
				out.println(getPage("Τα στοιχεία εισόδου είναι λανθασμένα."));
			}
			else
			{
				LoginCookie.Set(uid, password, rememberMe, response);
				response.sendRedirect("Profile");			
			}
		}
		else
		{
			out.println(getPage(validation));
		}
		
		out.close();

	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(LoginCookie.authCookie(request))
			response.sendRedirect("Profile");
		else
		{			
			response.setContentType("text/html; charset=UTF-8");	
			PrintWriter out = response.getWriter();
			
			out.println(getPage(""));
			
			out.close();
		}
	}

}
